//
//  XFXNewsHeadLineView.m
//  XFXnews
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXNewsHeadLineView.h"

@interface XFXNewsHeadLineView ()<UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, retain) NSTimer *myTimer;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, retain) UILabel *title;
@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation XFXNewsHeadLineView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }return self;
}

- (void)createSubViews{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, XFXWIDTH, XFXHEIGHT * 0.28)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
}

- (void)setArray:(NSMutableArray *)array{
    
    _array = array;
    if (array.count!=0) {
        NSLog(@"%ld",(unsigned long)array.count);
        for (int i = 0; i < array.count; i++) {
            XFXNewsHeadLineModel *model = [array objectAtIndex:i];
            UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(XFXWIDTH * i + XFXWIDTH, 0, XFXWIDTH, XFXHEIGHT * 0.28)];
            myImageView.tag = (i + 1) + 1000;
            [self.scrollView addSubview:myImageView];
            NSURL *url = [NSURL URLWithString:model.imgsrc];
            [myImageView sd_setImageWithURL:url placeholderImage:nil];
            self.title = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.01, XFXHEIGHT * 0.25, XFXWIDTH, 10 * XFXSHEI)];
            self.title.font = [UIFont systemFontOfSize:13];
            self.title.numberOfLines = 0;
            [myImageView addSubview:self.title];
            self.title.text = model.title;
            myImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            [myImageView addGestureRecognizer:tap];
        }
        // 取最后一张图加到数组
        XFXNewsHeadLineModel *model = [array objectAtIndex:array.count - 1];
        UIImageView *lastimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, XFXWIDTH, XFXHEIGHT * 0.28)];
        NSURL *url = [NSURL URLWithString:model.imgsrc];
        lastimage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionOne:)];
        [lastimage addGestureRecognizer:tap];
        [lastimage sd_setImageWithURL:url placeholderImage:nil];
        
        UILabel *lastTitle = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.01, XFXHEIGHT * 0.25, XFXWIDTH, 10 * XFXSHEI)];
        lastTitle.font = [UIFont systemFontOfSize:13];
        lastTitle.numberOfLines = 0;
        [lastimage addSubview:lastTitle];
        lastTitle.text = model.title;
        [_scrollView addSubview:lastimage];
        // 取第一张图
        UIImageView *firstimage= [[UIImageView alloc] initWithFrame:CGRectMake(XFXWIDTH * (array.count + 1), 0, XFXWIDTH, XFXHEIGHT * 0.28)];
        XFXNewsHeadLineModel *modelone = [array firstObject];
        NSURL *url1 = [NSURL URLWithString:modelone.imgsrc];
        [firstimage sd_setImageWithURL:url1 placeholderImage:nil];
        [self.scrollView addSubview:firstimage];
        firstimage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionTwo:)];
        [firstimage addGestureRecognizer:tapOne];
        UILabel *firstTitle = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.01, XFXHEIGHT * 0.25, XFXWIDTH, 10 * XFXSHEI)];
        firstTitle.font = [UIFont systemFontOfSize:13];
        firstTitle.numberOfLines = 0;
        [firstimage addSubview:firstTitle];
        firstTitle.text = modelone.title;
        
        id imageFirst = [self.array firstObject];
        id imageLast = [self.array lastObject];
        [self.array insertObject:imageLast atIndex:0];
        [self.array addObject:imageFirst];
        self.scrollView.contentSize = CGSizeMake(XFXWIDTH * (array.count + 2), 0);
        
    }
}

- (void)tapActionOne:(UITapGestureRecognizer *)tap{
    
    XFXNewsHeadLineModel *model =[self.array firstObject];
    [self.myDelegate showpushDetail:model];
}

- (void)tapActionTwo:(UITapGestureRecognizer *)tap{
    XFXNewsHeadLineModel *model =[self.array objectAtIndex:self.array.count - 1];
    
    [self.myDelegate showpushDetail:model];
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    XFXNewsHeadLineModel *model = [self.array objectAtIndex:tap.view.tag - 1000];
    [self.myDelegate showpushDetail:model];
    NSLog(@"self.allarray = %ld",(unsigned long)self.array.count);
    
}

#pragma mark -- 定时器
- (void)timerAction:(NSTimer *)timer{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [_scrollView setContentOffset:CGPointMake((self.currentIndex + 1) * XFXWIDTH, 0) animated:YES];
        
    }];
    self.currentIndex++;
    if (_scrollView.contentOffset.x == (self.array.count - 1) * XFXWIDTH) {
        
        [_scrollView setContentOffset:CGPointMake(XFXWIDTH, 0)];
        self.currentIndex = 1;
    }
}

#pragma  mark -- ScrollView协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.pageC.currentPage = scrollView.contentOffset.x / XFXWIDTH - 1;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == self.array.count * XFXWIDTH) {
        CGPoint temp = _scrollView.contentOffset;
        temp.x = XFXWIDTH * (self.array.count);
        _scrollView.contentOffset = temp;
    }
}


@end
